﻿using System.ComponentModel.DataAnnotations.Schema;
using System.Reflection;

namespace MicroOrm.Dapper.Repositories.SqlGenerator
{
    /// <summary>
    /// Metadata from PropertyInfo
    /// </summary>
    public class SqlPropertyMetadata
    {
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="propertyInfo">The property information.</param>
        public SqlPropertyMetadata(PropertyInfo propertyInfo)
        {
            _PropertyInfo = propertyInfo;
            var alias = _PropertyInfo.GetCustomAttribute<ColumnAttribute>();
            if (!string.IsNullOrEmpty(alias==null?"":alias.Name))
            {
                Alias = alias.Name;
                ColumnName = Alias;
            }
            else
            {
                ColumnName = _PropertyInfo.Name;
            }
        }

        /// <summary>
        /// Original PropertyInfo
        /// </summary>
        /// <value>The _ property information.</value>
        public PropertyInfo _PropertyInfo { get;set; }

        /// <summary>
        /// Alias for ColumnName,
        /// </summary>
        /// <value>The alias.</value>
        public string Alias { get; set; }

        /// <summary>
        /// ColumnName
        /// </summary>
        /// <value>The name of the column.</value>
        public string ColumnName { get; set; }

        /// <summary>
        /// PropertyName
        /// </summary>
        /// <value>The name of the property.</value>
        public string PropertyName {
        get{
            return _PropertyInfo.Name;
        }
        }
    }
}